import { Body, Controller, InternalServerErrorException, Logger, Post } from "@nestjs/common";
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from "@nestjs/swagger";
import { ShareContentDto, ShareRecordDto } from "./share.dto";
import { AuthUser } from "../auth/decorators/auth-user.decorator";
import { ShareService } from "./share.service";

@Controller("share")
@ApiTags("Share - 分享")
@ApiBearerAuth()
export class ShareController {
	private readonly logger = new Logger(ShareController.name);
	private readonly shareService: ShareService;

	// 记录用户分享出去的内容
	@Post("content")
	@ApiOperation({summary: "记录用户分享出去的内容"})
	@ApiBody({type: ShareContentDto})
	async recordShare(@Body() dto: ShareContentDto, @AuthUser() user: IAuthUser) {
		try {
			return await this.shareService.recordShare(dto, user.uid);
		}
		catch (error) {
			this.logger.error(error?.message ?? "获取分享内容失败");
			throw new InternalServerErrorException(error?.message ?? "获取分享内容失败");
		}
	}

	// 记录用户点击分享链接进入的记录
	@Post("record")
	@ApiOperation({summary: "记录用户点击分享链接进入的记录"})
	@ApiBody({type: ShareRecordDto})
	async recordShareEnter(@Body() dto: ShareRecordDto) {
		try {
			return await this.shareService.recordShareEnter(dto);
		}
		catch (error) {
			this.logger.error(error?.message ?? "记录分享进入失败");
			throw new InternalServerErrorException(error?.message ?? "记录分享进入失败");
		}
	}
}